<template>
  <div class="detailbox">
    <div class="flexbox">
      <div class="shrinkbox_v2">
        公司名称
      </div>
      <div class="contentbox_v2">
        <div class="div_input">
          {{ editData.enterpriseName }}
        </div>
      </div>
    </div>
    <div class="flexbox">
      <div class="shrinkbox_v2">
        密钥列表
      </div>
      <div class="contentbox_v2">
        <el-table :data="keyData" size="small">
          <el-table-column prop="key" label="黑锁密钥">
            <template #default="scope">
              <el-select
                class="diyselect"
                v-model="scope.row.key"
                filterable
                remote
                clearable
                reserve-keyword
                placeholder="请输入相应黑锁密钥"
                :remote-method="(val) => remoteMethod(val, scope.row, 0)"
                :loading="loading"
                >
                <el-option
                    v-for="item in keyList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="key" label="白锁密钥">
            <template #default="scope">
              <el-select
              class="diyselect"
              v-model="scope.row.bindSecretKeyId"
              filterable
              remote
              clearable
              reserve-keyword
              placeholder="请输入相应白锁密钥"
              :remote-method="(val) => remoteMethod_1(val, scope.row, 1)"
              :loading="loading_1"
              >
              <el-option
                  v-for="item in keyList_1"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
              />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="key" label="操作">
            <template #default="scope">
              <el-button @click="addLine(scope, index)" type="success" icon="Plus" circle size="small"/>
              <el-button @click="deleteLine(scope, index)" type="danger" icon="Minus" circle size="small"/>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { ref, inject, defineExpose, defineEmits } from 'vue'

  const feildConfig = ref({
    isReadonly: false
  })
  const emit = defineEmits(['close-dialog'])
  const editData = ref({
    enterpriseName: ''
  })

  const $utils = inject('$utils')
  const loading = ref(false)
  const optype = ref('')

  const keyData = ref([{
    key: ''
  }])
  const keyList = ref([])
  const keyList_1 = ref([])
  const loading_1 = ref(false)

  
  function initData (obj) {
    keyData.value = [{
      key: ''
    }]
    editData.value = obj.data ? obj.data : {}
    console.log(editData.value)
    optype.value = obj.type
  }

  
  function remoteMethod_1 (val, item, type) {
    loading_1.value = true
    $utils.post({
      url: '/api/SecretKeyInfo/GetWKeyInfoByAgentId',
      params: {
        keyWord: val
      },
      ok: function (data) {
          keyList_1.value = data.data
          loading_1.value = false
        // this.$emit('close-dialog', false)
      }
    })
  }

  function remoteMethod (val, item, type) {
    loading.value = true
    $utils.post({
      url: '/api/SecretKeyInfo/GetBKeyInfoByAgentId',
      params: {
        keyWord: val
      },
      ok: function (data) {
          keyList.value = data.data
          loading.value = false
        // this.$emit('close-dialog', false)
      }
    })
  }
  
  // 新增行
  function addLine (item, index) {
    keyData.value.splice((item.$index + 1), 0, { key: '' })
  }
  // 删除行
  function deleteLine (item, index) {
    $utils.messageBox.confirm('是否删除密钥：' + item.row.key, '提示').then(() => {
      keyData.value.splice(item.$index, 1)
      if (keyData.value.length == 0) {
        keyData.value = [{
          key: ''
        }]
      }
    }).catch(() => {

    })
    
  }

  function submitData () {
    const submitJson = {}
    submitJson.enterpriseId = editData.value.id
    submitJson.secretKeyIds = []
    submitJson.secretKeyIds_1 = []
    for (let i = 0; i < keyData.value.length; i++) {
      submitJson.secretKeyIds.push(keyData.value[i].key)
      submitJson.secretKeyIds_1.push(keyData.value[i].bindSecretKeyId?keyData.value[i].bindSecretKeyId:'')
    }
    console.log(submitJson)
    $utils.post({
      url: '/api/SecretKeyInfo/DoBatchDispatch',
      params: submitJson,
      ok: function (data) {
        emit('close-dialog', false)
      }
    })
  }

  defineExpose({
    initData,
    submitData
  })
</script>

<style scoped>
.saveBtn {
  background-color: #4999FA;
  color: #fff;
  font-weight: normal;
  font-size: 14px;
  padding: 3px 10px;
  border-radius: 3px;
  margin-right: 10px;
}
.saveBtn:hover {
  /* background-color: #7BB8FB; */
  opacity: 0.8;
  cursor: pointer;
}
.split_title {
  background-color: #EBEEF5;
  font-size: 16px;
  height: 30px;
  align-items: center;
  font-family: '楷体';
  font-weight: bold;
  display: flex;
  position: relative;
}
.split_title_content {
  flex-grow: 1;
  text-align: center;
}
.split_title_btn {
  display: flex;
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  align-items: center;
}
.shrinkbox_v2 {
  width: 70px;
}
.diyinput :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diyselect :deep(.el-select__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diyselect :deep(.el-select__wrapper:hover){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyselect :deep(.el-select__wrapper:active){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
/* .diyselect :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
  pointer-events: none; 
}
.diyselect :deep(.el-input__wrapper:hover){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyselect :deep(.el-input__wrapper:active){
  border: 0 !important;
  outline: none;
  box-shadow: none;
} */
.diyinput :deep(.el-textarea__inner){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diydatepicker :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
  font-size: 12px;
}
.diyformitem {
  margin-bottom: 0;
}
.diydatapicker :deep(.el-date-editor){
  width: 100%;
}
.diydatapicker :deep(.el-input__wrapper){
  border: 0 !important;
  outline: none;
  box-shadow: none;
}
.diyinput :deep(.el-input-group__append){
  border: 0 !important;
  outline: none;
  background-color: unset;
  box-shadow: none;
  padding: 0 10px 0 5px;
  font-weight: bold;
  font-size: 16px;
}
/deep/ input::-webkit-outer-spin-button,
/deep/ input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}
/deep/ input[type='number'] {
  -moz-appearance: textfield;
}

</style>